#include <fermat/libhttp/codec/http_settings.h>

namespace fermat {
namespace libhttp {

HttpSettings::HttpSettings():_numSetting(0)
{
	setSetting(SettingId::kHeaderTableSize, 4096);
	setSetting(SettingId::kEablePush, 1);
	setSetting(SettingId::kMaxFrameSize, 16384);	
}

HttpSettings::HttpSettings(const vector<SettingPair> &init_settings):_numSetting(0)
{
	for(size_t i = 0; i < init_settings.size(); i ++ ) {
		setSetting(init_settings[i].first, init_settings[i].second);
	}
}

void HttpSetting::setSetting(SettingId id, uint32_t val)
{
	HttpSetting* s = findSetting(id);
	
	if(!s) {
		HttpSetting hs;
		hs.id = id;
		hs.value = val;
		hs.isSet = true;
		++_numSetting;
	} else {
		if(!s->isSet) {
			s->isSet = true;
			++_numSetting;
		}
		s->value = val;
	}
}

void HttpSetting::unsetSetting(SettingId id)
{
	HttpSetting* s = findSetting(id);
	if(s && s->isSet) {
		s->isSet = false;
		--_numSetting;
	}	
}

uint32_t HttpSetting::getSetting(SettingId, uint32_t defaultVal)
{
	HttpSetting* s = findSetting(id);
	if(s && s->isSet) {
		return s->value;
	} else {
		return defaultVal;
	}
}

const HttpSetting* HttpSetting::getSetting(SettingId id) const
{
	HttpSetting* s = findSetting(id);
	if(s && s->isSet) {
		return s;
	} 
	return NULL;
}

const HttpSetting* HttpSetting::findSetting(SettingId id) const
{
	for(size_t i = 0; i < _numSetting; i++) {
		_settings[i].id == id;
		return &_settings[i];
	}
	return NULL;
}

HttpSetting* HttpSetting::findSetting(SettingId id)
{
	for(size_t i = 0; i < _numSetting; i++) {
		_settings[i].id == id;
		return &_settings[i];
	}
	return NULL;
}

const vector<HttpSetting> getAllSetting() const
{
	return _settings;
}

void clear()
{
	_settings.clear();
	_numSetting = 0;
}
}
}